<?php
define('APP_PATH',__DIR__);
define('DS',DIRECTORY_SEPARATOR);
/*
 * 创建文件夹函数
 * dirPath 要上传文件的目录
 * realPath 真实的上传目录
 * 
*/
if(!function_exists('creaDir')){
    function creaDir($dirPath=''){
        $realPath = APP_PATH.DS.$dirPath;
        //|| 左边的符合条件的话   就不走右边了   
        if(is_dir($realPath) || mkdir($realPath,0777,true)){
            return $dirPath;
        }
    }
}

/*
 *上传图片
 *upFile 上传文件的资源
 *DirName 上传文件的路径
*/
if(!function_exists('uploadToLocal')){
    function uploadToLocal($upFile,$DirName){
        //上传图片的限制
        //允许被上传文件的后缀
        $allowExts = ["gif","jpeg","jpg","png"];
        //允许上传图片的类型
        $allowedImgTypes = ["image/gif","image/jpeg","image/jpg","image/png","image/pjpeg","image/x-png"];
        $size = 204800;
        $temp = explode('.',$upFile['name']);
        //获取文件的后缀  end 获取数组的最后一个
        $extension = end($temp);
        $boolean = 
                !empty($upFile) 
                && in_array($upFile['type'],$allowedImgTypes)
                && in_array($extension,$allowExts)
                && $upFile['size'] < $size
                && $upFile['error'] == 0
        ;
        //代表上传的文件都是符合条件的
        if($boolean){
            $dirPath = creaDir($DirName);
            $filename = $upFile['name'];
            $filename = time().$filename;
            //真实的上传图片的地址
            $queryPath = APP_PATH.DS.$dirPath.DS.$filename;
            if(move_uploaded_file($upFile['tmp_name'],$queryPath)){
                return json_encode(['status'=>1,'msg'=>'上传成功','path'=>DS.$dirPath.DS.$filename]);
            }else{
                return json_encode(['status'=>-1,'msg'=>'文件上传失败']);
            }
        }else{
            return json_encode(['status'=>-1,'msg'=>'文件上传失败',$upFile['error']]);
        }

    }
}